/*
* $Id: ChangeFamilyWizard.java 272 2011-02-21 05:37:35Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.component.journal;

import java.util.Vector;

import apiary.Apiary;
import apiary.db.FamilyStore.FamilyRecord;
import apiary.db.FieldsValueStore.FieldsValueRecord;
import apiary.db.managers.ConvertData;
import apiary.db.managers.DBManager;
import apiary.ui.BackScreen;
import apiary.common.FormHelper;
import apiary.ui.journal.*;
import apiary.ui.journal.ChooseForm.ChooseValue;
import apiary.ui.journal.FamilyForm.FamilyValue;
import apiary.ui.journal.UserFieldForm.UserField;

/**
 * Wizard of changing family
 * @author lexus
 */
public class ChangeFamilyWizard extends Wizard
{
    /**
     * Constructor
     */
    public ChangeFamilyWizard() {
        super(null);
        Init();
    }

    /**
     * Constructor
     * @param parentForm Form where it want to move when pressing Back.
     * This form used only on first form of wizard
     */
    public ChangeFamilyWizard(BackScreen parentForm) {
        super(null);
        Init();
        setCancelForm(parentForm);
    }

    /**
     * Saves result of wizard
     */
    public void save() {
        ChooseValue familyId = (ChooseValue) formData.get(new Integer(2));
        FamilyValue family = (FamilyValue) formData.get(new Integer(3));
        DBManager.getInstance().changeFamily(familyId.id, family);
    }

    /**
     * Calls before to show next form of wizard
     * @param nextForm next form of wizard
     */
    public void next(BackScreen nextForm) {
        WizardScreen form = (WizardScreen) nextForm;
        Integer idForm = form.getIdForm();
        if(idForm.intValue() == 2) {
            ChooseValue point = (ChooseValue) formData.get(new Integer(1));
            FormHelper.fillFamilyList(point.id, (ChooseForm) form);
        } else if(idForm.intValue() == 3) {
            ChooseValue family = (ChooseValue) formData.get(new Integer(2));
            setFamilyData(family.id, (FamilyForm) form);
        }
    }

    /**
     * Checks data of form on truth
     * @param dataForm data of form
     * @return truth of data
     */
    public boolean isOk(Integer idForm, Object dataForm) {
        return true;
    }

    /**
     * Form is filled data family
     * @param idFamily identificator of family
     * @param familyForm form for fill
     */
    private void setFamilyData(int idFamily, FamilyForm familyForm) {
        DBManager dBManager = DBManager.getInstance();
        FamilyRecord family = dBManager.getFamily(idFamily);
        FamilyValue value = familyForm.new FamilyValue();
        ConvertData.FamilyRecordToValue(family, value);
        Vector fieldsValue = dBManager.getFieldsValue(family.idRecord);
        Vector userFields = new Vector();
        for(int i = 0; i < fieldsValue.size(); i++) {
            FieldsValueRecord fieldValue = (FieldsValueRecord) fieldsValue.elementAt(i);
            UserField userField = familyForm.new UserField();
            ConvertData.FieldsValueToUserField(fieldValue, userField);
            userFields.addElement(userField);
        }
        value.userFields = userFields;
        familyForm.setFamilyValue(value);
    }

    /**
     * Initialize Wizard
     */
    private void Init() {
        Integer idChoose = new Integer(1);
        Apiary apiary = Apiary.getInstance();
        firstForm = new ChooseForm(apiary.getLocaleString("ChoosePoint"), idChoose, this, true);
        FormHelper.fillPointList((ChooseForm) firstForm);
        Integer idFamily = new Integer(2);
        ChooseForm chooseForm = new ChooseForm(apiary.getLocaleString("ChooseFamily"), idFamily, this, true);
        chooseForm.setBackScreen(firstForm);
        formsNext.put(idChoose, chooseForm);
        Integer idChange = new Integer(3);
        FamilyForm familyForm = new FamilyForm(apiary.getLocaleString("ChangeFamily"), idChange, this, false);
        familyForm.setBackScreen(chooseForm);
        formsNext.put(idFamily, familyForm);
    }
}
